package com.tool;

import lombok.SneakyThrows;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class ToolRemote {

    
    public static boolean pingIp(String ip) {
        //能ping通放回true 反之 false 超时时间 3000毫秒
        try {
            return InetAddress.getByName(ip).isReachable(5000);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean telnetPort(String ip, Integer port) {
        Socket connect = new Socket();
        boolean res = false;
        try {
            connect.connect(new InetSocketAddress(ip, port), 1000);//建立连接
            //能telnet通返回true，否则返回false
            res = connect.isConnected();//通过现有方法查看连通状态
        } catch (IOException e) {
        } finally {
            try {
                connect.close();
            } catch (IOException e) {
            }
        }
        return res;
    }
}
